#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        validView.py
# Author:      KuKei
# Create Date: 2011-04-18
# Description: 验证视图
# Modify Date: 2011-04-18

from sd.cms.views import BaseView
from sd.cms.forms.sysForm import UserForm, LoginForm

class ValidView(BaseView):
    def valid(self, **args):
        try:
            validForm = eval(args['view']+'Form()')
            name = self.request.POST['_field'] if self.request.POST.has_key('_field') else None
            if name:
                value = self.request.POST['_value'] if self.request.POST.has_key('_value') else None
                validForm.fieldClean(name, value)
                self.result.flag = True
                self.result.msg = validForm.errors[name][0] if validForm.errors.has_key(name) else '验证成功'
                self.result.html = validForm.errors[name].as_ul() if validForm.errors.has_key(name) else '验证成功'
        except Exception, e:
            self.result.flag = False
            self.result.msg = e.message
        return self.render_json()

def action(request, **args):
    return apply(ValidView(request).valid, (), args)